/*  
 * @(#) DemoUserServiceImpl.java Create on 2012-5-9 下午5:31:51   
 *   
 * Copyright 2012 by xl.   
 */

package cn.com.demo.annotation.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import cn.com.common.base.bean.BackstageResult;
import cn.com.common.base.bean.PageBean;
import cn.com.demo.annotation.service.DemoAnnotationUserService;
import cn.com.demo.bean.DemoUserBean;
import cn.com.demo.dao.DemoUserDao;

/**
 * 
 * @author zhanghongliang
 * @date 2012-5-9
 */
@Service
public class DemoAnnotaionUserServiceImpl implements DemoAnnotationUserService {
	@Resource
	DemoUserDao dao;
 
	@Override
	public PageBean<DemoUserBean> getUserPage(DemoUserBean query) throws Exception {
		@SuppressWarnings("unchecked")
		Map<String, Object> params = BeanUtils.describe(query);
		return dao.getPageBean(params, DemoUserBean.class);
	}

	@Override
	public DemoUserBean find(Integer id) throws Exception {
		DemoUserBean result = null;
		if (id != null && id > 0) {
			result = dao.get(id, DemoUserBean.class);
		}
		if (result == null) {
			result = new DemoUserBean();
			result.setId(-1);
			return result;
		}
		return result;
	}

	@Override
	public BackstageResult deltet(DemoUserBean oprBean) throws Exception {
		int oprCount = dao.delete(oprBean);
		return new BackstageResult(true,oprCount);
	}

	@Override
	public BackstageResult save(DemoUserBean oprBean) throws Exception {
		int result = 0;
		if(oprBean.getId()!=null && oprBean.getId()>0){
			result = this.dao.updateNotNull(oprBean);
		}else{
			result = (int) this.dao.insert(oprBean, false);
		}
		return new BackstageResult(true, result);
	}
}
